/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package send;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class AnimatingSheet extends JPanel {

    Dimension animatingSize = new Dimension(0, 1);
    JComponent source;
    BufferedImage offscreenImage;

    public AnimatingSheet() {
        super();
        setOpaque(true);
    }

    public void setSource(JComponent source) {
        this.source = source;
        animatingSize.width = source.getWidth();
        makeOffscreenImage(source);
    }

    public void setAnimatingHeight(int height) {
        animatingSize.height = height;
        setSize(animatingSize);
    }

    private void makeOffscreenImage(JComponent source) {
        GraphicsConfiguration gfxConfig =
                GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        offscreenImage =
                gfxConfig.createCompatibleImage(source.getWidth(),
                source.getHeight());
        Graphics2D offscreenGraphics =
                (Graphics2D) offscreenImage.getGraphics();
        source.paint(offscreenGraphics);
    }

    public Dimension getPreferredSize() {
        return animatingSize;
    }

    public Dimension getMinimumSize() {
        return animatingSize;
    }

    public Dimension getMaximumSize() {
        return animatingSize;
    }

    public void paint(Graphics g) {
        // get the bottom-most n pixels of source and 
        // paint them into g, where n is height

        BufferedImage fragment =
                offscreenImage.getSubimage(
                offscreenImage.getHeight(),animatingSize.height,
        source.getWidth(),animatingSize.height);
			// g.drawImage (fragment, 0, 0, this); 
			g.drawImage (fragment, 0, 0, this);
    }
}
